﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SQLite;

namespace WebSocket.App_Code.Common
{
    public class SQLiteHelper
    {
        /// <summary>
        /// 数据连接
        /// </summary>
        private static readonly SQLiteConnection conn = Global.SQLiteConn;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="comm"></param>
        /// <param name="conn"></param>
        /// <param name="CommandText"></param>
        /// <param name="cmdType"></param>
        /// <param name="arg"></param>
        private static void AddParameter(SQLiteCommand comm, SQLiteConnection conn, string CommandText, CommandType cmdType, SQLiteParameter[] arg)
        {
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            comm.Parameters.Clear();
            comm.Connection = conn;
            comm.CommandText = CommandText;
            comm.CommandType = cmdType;
            if (arg != null)
            {
                foreach (SQLiteParameter obj in arg)
                {
                    comm.Parameters.Add(obj);
                }
            }
        }

        /// <summary>
        /// ExecuteScalar
        /// </summary>
        /// <param name="CommandText"></param>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string CommandText, SQLiteParameter[] arg)
        {
            using (SQLiteCommand comm = new SQLiteCommand())
            {
                try
                {
                    AddParameter(comm, conn, CommandText, CommandType.Text, arg);
                    object obj = comm.ExecuteScalar();
                    return obj;
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(ex.Message);
                }
            }
        }

        /// <summary>
        /// ExecuteNonQuery
        /// </summary>
        /// <param name="CommandText"></param>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string CommandText, SQLiteParameter[] arg)
        {
            using (SQLiteCommand comm = new SQLiteCommand())
            {
                try
                {
                    AddParameter(comm, conn, CommandText, CommandType.Text, arg);
                    int res = comm.ExecuteNonQuery();
                    return res;
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(ex.Message);
                }
            }
        }

        /// <summary>
        /// 得到DataSet
        /// </summary>
        /// <param name="CommandText"></param>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static DataSet GetDataSet(string CommandText,SQLiteParameter[] arg)
        {
            using (SQLiteCommand comm = new SQLiteCommand())
            {
                try
                {
                    AddParameter(comm, conn, CommandText, CommandType.Text, arg);
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(comm);
                    DataSet ds = new DataSet();
                    adapter.Fill(ds);
                    return ds;
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(ex.Message);
                }
            }
        }
    }
}